<template>
  <div class="app-container">
    <el-form ref="placeCodeForm" v-loading="formLoading" :rules="rules" :model="form" label-position="top">
      <el-row type="flex" :gutter="20">
        <el-col :span="24">
          <el-form-item label="分类名称" prop="name" style="width: 400px">
            <el-input v-model="form.name" placeholder="请输入标题" clearable  />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="父级分类" prop="fatherId" style="width: 400px">
            <el-cascader
              v-model="form.fatherId"
              placeholder="请选择文章分类"
              :options="fatherOption"
              :props="{value:'id',label:'name'}"
              filterable></el-cascader>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import {editNewsTypeConfigApi, getFatherList} from "@/api/newsTypeConfig";
export default {
  name: 'PlaceCodeDetail',
  data(){
    return{
      editor: null,
      form: {
        id: null,
        name:'',
        fatherId: 0,
        conLevel:0,
        conType: 6,
      },
      rules: {
        name: [{ required: true, trigger: 'blur', message: '分类名称' }]

      },
      formLoading: false,
      fatherOption: [],
    }
  },
  props:['id','closeDialog'],
  methods:{

    submit(){
      this.$refs.placeCodeForm.validate(valid => {
        if (valid) {
          const loading=this.$loading({
            text:'提交中'
          })
          const form=JSON.parse(JSON.stringify(this.form));
          form.conLevel = form.fatherId>0 ? 2 : 1;
          if(typeof this.form.fatherId!=='number'){
            form.fatherId=this.form.fatherId[0];
          }
          editNewsTypeConfigApi(form).then((res)=>{
            this.$message.success('保存成功');
            this.closeDialog(0);
          }).catch(()=>{
            this.$message.error('保存失败');
          }).finally(()=>{
            loading.close();
          })
        } else {
          return false
        }
      })
    },
    getFatherList(){
      this.formLoading=true;
      getFatherList({}).then((res)=>{
        this.fatherOption=res;
      }).finally(()=>{
        this.formLoading=false;
      })
    },
  },
  mounted() {
      this.getFatherList();
  },
}
</script>
